package com.cskaoyan.service.admin.usermanage.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.admin.auth.Md5Util;
import com.cskaoyan.bean.vo.admin.usermanage.UsesrManageListVo;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.admin.usermanage.UserManageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author MoJunXiang
 * @since 2022/09/07 14:05
 */
@Service
public class UserManageServiceImpl implements UserManageService {


    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;


    /**
     * @function_name userlist
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @idea 返回**所有**用户信息
     * @return com.cskaoyan.bean.vo.admin.usermanage.UsesrManageListVo
     * @author MoJunXiang
     * @since 2022/09/08 11:49
     */
    @Override
    public UsesrManageListVo userlist(Integer page, Integer limit, String username, String mobile, String sort, String order) {
        // 返回值
        UsesrManageListVo usesrManageListVo = new UsesrManageListVo();

        PageHelper.startPage(page, limit);
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        List<MarketUser> poList = marketUserMapper.selectByExample(example);
        PageInfo<MarketUser> pageInfo = new PageInfo<>(poList);
        int pages = pageInfo.getPages();
        int total = (int) pageInfo.getTotal();

        usesrManageListVo.setLimit(limit);
        usesrManageListVo.setList(poList);
        usesrManageListVo.setPage(page);
        usesrManageListVo.setPages(pages);
        usesrManageListVo.setTotal(total);

        return usesrManageListVo;
    }







    /**
     * @function_name addresslist
     * @param page
     * @param limit
     * @param name
     * @param userId
     * @param sort
     * @param order
     * @idea 若 *name* ， *userId* 为空，则返回 **所有用户** 的 **所有地址** ；否则则 模糊查询  *name* ， 精确查询  *userId* ，返回对应的 **地址**
     * @return com.cskaoyan.bean.vo.admin.usermanage.UsesrManageListVo
     * @author MoJunXiang
     * @since 2022/09/13 9:48
     */
    @Override
    public UsesrManageListVo addresslist(Integer page, Integer limit, String name, Integer userId, String sort, String order) {
        // 返回值
        UsesrManageListVo<MarketAddress> listVo = new UsesrManageListVo<>();

        PageHelper.startPage(page, limit);
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketAddress> list = marketAddressMapper.selectByExample(example);
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(list);

        listVo.setLimit(limit);
        listVo.setList(list);
        listVo.setPage(page);
        listVo.setPages(pageInfo.getPages());
        listVo.setTotal(((int) pageInfo.getTotal()));

        return listVo;
    }


    /**
     * @function_name collectlist
     * @param page
     * @param limit
     * @param userId
     * @param valueId
     * @param sort
     * @param order
     * @idea 若 *userId* 与 *valueId* 为空，则返回**会员**的所有**收藏**；否则，返回 **market_collect表** 里对应 *userId* 和 *valueId* 的数据
     * @return com.cskaoyan.bean.vo.admin.usermanage.UsesrManageListVo
     * @author MoJunXiang
     * @since 2022/09/13 10:13
     */
    @Override
    public UsesrManageListVo collectlist(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        PageHelper.startPage(page, limit);
        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        List<MarketCollect> list = marketCollectMapper.selectByExample(example);
        PageInfo<MarketCollect> pageInfo = new PageInfo<>(list);

        // 返回值
        UsesrManageListVo<MarketCollect> listVo = new UsesrManageListVo<>();
        listVo.setLimit(limit);
        listVo.setList(list);
        listVo.setPage(page);
        listVo.setPages(pageInfo.getPages());
        listVo.setTotal(((int) pageInfo.getTotal()));

        return listVo;
    }



    /**
     * @function_name footprintlist
     * @param page
     * @param limit
     * @param userId
     * @param goodsId
     * @param sort
     * @param order
     * @idea 若 *userId* 与 *goodsId* 为空，则返回**会员**的所有**足迹**；否则，返回 **market_footprint表** 里对应 *userId* 和 *goodsId* 的数据
     * @return com.cskaoyan.bean.vo.admin.usermanage.UsesrManageListVo
     * @author MoJunXiang
     * @since 2022/09/13 10:23
     */
    @Override
    public UsesrManageListVo footprintlist(Integer page, Integer limit, Integer userId, Integer goodsId, String sort, String order) {
        PageHelper.startPage(page, limit);
        MarketFootprintExample example = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        List<MarketFootprint> list = marketFootprintMapper.selectByExample(example);
        PageInfo<MarketFootprint> pageInfo = new PageInfo<>(list);

        UsesrManageListVo<MarketFootprint> listVo = new UsesrManageListVo<>();
        listVo.setLimit(limit);
        listVo.setTotal(((int) pageInfo.getTotal()));
        listVo.setPages(pageInfo.getPages());
        listVo.setPage(page);
        listVo.setList(list);

        return listVo;
    }


    /**
     * @function_name historylist
     * @param page
     * @param limit
     * @param userId
     * @param keyword
     * @param sort
     * @param order
     * @idea 若 *userId* 与 *keyword* 为空，则返回**会员**的所有**搜索历史**；否则，返回 **market_search_history表** 里对应 *userId* 和 *keyword* 的数据返回所有**会员**的所有**搜索历史**
     * @return com.cskaoyan.bean.vo.admin.usermanage.UsesrManageListVo
     * @author MoJunXiang
     * @since 2022/09/13 10:30
     */
    @Override
    public UsesrManageListVo historylist(Integer page, Integer limit, Integer userId, String keyword, String sort, String order) {
        PageHelper.startPage(page, limit);
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        List<MarketSearchHistory> list = marketSearchHistoryMapper.selectByExample(example);
        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(list);


        // 返回值
        UsesrManageListVo<MarketSearchHistory> listVo = new UsesrManageListVo<>();
        listVo.setList(list);
        listVo.setPage(page);
        listVo.setPages(pageInfo.getPages());
        listVo.setTotal(((int) pageInfo.getTotal()));
        listVo.setLimit(limit);

        return listVo;
    }


    /**
     * @function_name feedbacklist
     * @param page
     * @param limit
     * @param username
     * @param sort
     * @param order
     * @param id
     * @idea 若 *id* 与 *username* 为空，则返回**会员**的所有**反馈**；否则，返回 **market_feedback表** 里对应 *id* 和 *username* 的数据返回
     * @return com.cskaoyan.bean.vo.admin.usermanage.UsesrManageListVo
     * @author MoJunXiang
     * @since 2022/09/13 10:56
     */
    @Override
    public UsesrManageListVo feedbacklist(Integer page, Integer limit, String username, String sort, String order, Integer id) {
        PageHelper.startPage(page, limit);
        MarketFeedbackExample example = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = example.createCriteria();
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        List<MarketFeedback> list = marketFeedbackMapper.selectByExample(example);
        PageInfo<MarketFeedback> pageInfo = new PageInfo<>(list);

        // 返回值
        UsesrManageListVo<MarketFeedback> listVo = new UsesrManageListVo<>();
        listVo.setList(list);
        listVo.setPage(page);
        listVo.setPages(pageInfo.getPages());
        listVo.setTotal(((int) pageInfo.getTotal()));
        listVo.setLimit(limit);

        return listVo;
    }


    /**
     * @function_name detail
     * @param id
     * @idea 根据传入的 *id* 在 **market_user表** 中找到对应 *id* 的数据并返回它
     * @return com.cskaoyan.bean.MarketUser
     * @author MoJunXiang
     * @since 2022/09/13 9:40
     */
    @Override
    public MarketUser detail(Integer id) {
        MarketUser user = null;

        user = marketUserMapper.selectByPrimaryKey(id);

        return user;
    }



    @Override
    public void update(MarketUser user) throws Exception {
        String password = user.getPassword();
        String pwd = Md5Util.getMd5(password);
        user.setPassword(pwd);
        marketUserMapper.updateByPrimaryKeySelective(user);
    }
}
